package top.fullj.actor;

/**
 * Actor reference class
 *
 * @author wurenhai
 * @since 2018/3/11
 */
public class ActorRef {

    /**
     * none actor
     */
    public static final ActorRef NONE = null;

    /**
     * no sender
     */
    public static final ActorRef NO_SENDER = null;

    public ActorPath path() {
        return this.ctx.path;
    }

    public boolean isTerminated() {
        return this.ctx.isTerminated();
    }

    public void forward(ActorRef sender, Object message) {
        tell(message, sender);
    }

    public void tell(Object message) {
        tell(message, NO_SENDER);
    }

    public void tell(Object message, ActorRef sender) {
        if (!ctx.isTerminated()) {
            ctx.enqueue(new ActorContext.Delivery(message, sender));
        } else {
            ctx.system.deadLetters.tell(message, sender);
        }
    }

    /**
     * my context
     */
    public final ActorContext ctx;

    ActorRef(ActorContext ctx) {
        this.ctx = ctx;
    }

    @Override
    public String toString() {
        return ctx.path.toString();
    }

}
